/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.aergia.vinny.core.Version;
import org.aergia.vinny.util.IconUtil;

public class AboutDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JButton closeButton;
	private JLabel copyrightLabel;
	private JButton creditButton;
	private JButton licenseButton;
	private final String productName;
	private JLabel versionLabel;
	private JLabel vinnyIcon;
	private JLabel vinnyLabel;

	public AboutDialog(final Frame parent, final String productName) {
		super(parent);
		this.productName = productName;
		this.initComponents();
		this.pack();
	}

	@Override
	public void actionPerformed(final ActionEvent e) {
		if (e.getSource() == this.closeButton) {
			this.setVisible(false);
		}
		if (e.getSource() == this.licenseButton) {
			final StringBuilder sb = new StringBuilder();
			sb.append("Vinny is free software; you can redistribute it and/or modify it under the\n");
			sb.append("terms of the GNU General Public License as published by the Free Software\n");
			sb.append("Foundation; either version 3 of the License; or (at your option) any later\n");
			sb.append("version.\n");
			sb.append("\n");
			sb.append("Vinny is distributed in the hope that it will be useful, but WITHOUT ANY\n");
			sb.append("WARRANTY; without even the implied warranty of MERCHANTABILITY or\n");
			sb.append("FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n");
			sb.append("for more details.\n");
			sb.append("\n");
			sb.append("You should have received a copy of the GNU General Public License along\n");
			sb.append("with Vinny; if not, write to Free Software Foundation, Inc., 59 Temple\n");
			sb.append("Place - Suite 330, Boston, MA 02111-1307, USA.\n");

			JOptionPane.showMessageDialog(this, sb.toString(), "License", JOptionPane.INFORMATION_MESSAGE);
		}
		if (e.getSource() == this.creditButton) {
			final StringBuilder sb = new StringBuilder();
			sb.append("Written by: Fred Barrie (barrie@acm.org)\n");
			sb.append("JCalendar from http://flib.sourceforge.net/JCalendar/doc/index.html");
			JOptionPane.showMessageDialog(this, sb.toString(), "Credits", JOptionPane.INFORMATION_MESSAGE);
		}

	}

	private JLabel getCopyrightLabel() {
		if (this.copyrightLabel == null) {
			this.copyrightLabel = new JLabel();
			this.copyrightLabel.setFont(new Font("Dialog", Font.ITALIC, 12));
			this.copyrightLabel.setHorizontalAlignment(SwingConstants.CENTER);
			this.copyrightLabel.setText("Copyright \u00a9 2010-2011 Fred Barrie and the Vinny Project Team");
		}
		return this.copyrightLabel;
	}

	private JButton getCreditButton() {
		if (this.creditButton == null) {
			this.creditButton = new JButton();
			this.creditButton.setText("Credits");
			this.creditButton.addActionListener(this);
		}
		return this.creditButton;
	}

	private JButton getLicenseButton() {
		if (this.licenseButton == null) {
			this.licenseButton = new JButton();
			this.licenseButton.setText("License");
			this.licenseButton.addActionListener(this);
		}
		return this.licenseButton;
	}

	private JLabel getVersionLabel() {
		if (this.versionLabel == null) {
			this.versionLabel = new JLabel();
			this.versionLabel.setFont(new Font("Arial", Font.BOLD | java.awt.Font.ITALIC, 24));
			this.versionLabel.setHorizontalAlignment(SwingConstants.CENTER);
			this.versionLabel.setText(this.productName + " " + Version.getVersion());
		}
		return this.versionLabel;
	}

	private JLabel getVinnyIcon() {
		if (this.vinnyIcon == null) {
			this.vinnyIcon = new JLabel("");
			this.vinnyIcon.setIcon(IconUtil.getIcon("/images/64/vinny.png"));
		}
		return this.vinnyIcon;
	}

	private JLabel getVinnyLabel() {
		if (this.vinnyLabel == null) {
			this.vinnyLabel = new JLabel();
			this.vinnyLabel.setFont(new Font("Arial", Font.BOLD, 16));
			this.vinnyLabel.setHorizontalAlignment(SwingConstants.CENTER);
			this.vinnyLabel.setText("A Vinny Project");
		}
		return this.vinnyLabel;
	}

	private void initComponents() {

		this.setTitle("About " + this.productName);
		this.setFont(new Font("Dialog", Font.PLAIN, 12));
		this.setBackground(Color.white);
		this.setForeground(Color.black);
		this.setLayout(new BorderLayout());

		final JPanel titlePanel = new JPanel(new BorderLayout());
		titlePanel.add(this.getVersionLabel(), BorderLayout.NORTH);
		titlePanel.add(this.getVinnyLabel(), BorderLayout.SOUTH);

		final JPanel titleAndButtonPanel = new JPanel(new BorderLayout());
		titleAndButtonPanel.add(this.getVinnyIcon(), BorderLayout.WEST);
		titleAndButtonPanel.add(titlePanel, BorderLayout.CENTER);

		final JPanel titleAndCopyrightPanel = new JPanel(new BorderLayout());
		titleAndCopyrightPanel.add(titleAndButtonPanel, BorderLayout.CENTER);
		titleAndCopyrightPanel.add(this.getCopyrightLabel(), BorderLayout.SOUTH);

		final JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
		buttonPanel.add(new JLabel());
		buttonPanel.add(this.getCreditButton());
		buttonPanel.add(this.getLicenseButton());

		this.add(titleAndCopyrightPanel, BorderLayout.NORTH);
		this.add(buttonPanel, BorderLayout.SOUTH);

		this.setSize(373, 184);
	}

}
